﻿using System.Collections.ObjectModel;
using System.ComponentModel;

namespace _508ComplianceValidator
{
    public class CheckableItem : INotifyPropertyChanged
    {
        private ObservableCollection<CheckableItem> _children = new ObservableCollection<CheckableItem>();
        private bool _isChecked;

        /// <summary>
        /// Gets or sets the value.
        /// </summary>
        /// <value>
        /// The value.
        /// </value>
        public string Value { get; set; }

        /// <summary>
        /// Gets or sets the display text.
        /// </summary>
        /// <value>
        /// The display text.
        /// </value>
        public string DisplayText { get; set; }

        /// <summary>
        /// Gets or sets the children.
        /// </summary>
        /// <value>
        /// The children.
        /// </value>
        public ObservableCollection<CheckableItem> Children
        {
            get { return _children; }
            set { _children = value; }
        }

        /// <summary>
        /// Gets or sets a value indicating whether this instance is checked.
        /// </summary>
        /// <value>
        /// 	<c>true</c> if this instance is checked; otherwise, <c>false</c>.
        /// </value>
        public bool IsChecked
        {
            get { return _isChecked; }
            set
            {
                foreach (CheckableItem child in Children)
                {
                    child.IsChecked = value;
                }
                if (Children.Count == 0)
                {
                    if (!value && MainWindow.SelectedFiles.Contains(this))
                        MainWindow.SelectedFiles.Remove(this);
                    else if (value && !MainWindow.SelectedFiles.Contains(this))
                    {
                        MainWindow.SelectedFiles.Add(this);
                    }
                }
                _isChecked = value;
                OnPropertyChanged("IsChecked");
            }
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        /// <summary>
        /// Called when [property changed].
        /// </summary>
        /// <param name="propertyName">Name of the property.</param>
        private void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}